<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('服务单项')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">

        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            是否套餐：
                            <select name="packageStatus" th:with="type=${@dict.getType('si_car_package')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            服务分类：
                            <select name="catalogStatus" th:with="type=${@dict.getType('si_service_catalog')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            审核状态：
                            <select name="auditStatus" th:with="type=${@dict.getType('si_audit_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            上架状态：
                            <select name="saleStatus" th:with="type=${@dict.getType('si_sale_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:post:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-warning auditBtn disabled" id="examine" onclick="startAudit()" >
                <i class="fa fa-download"></i> 发起审核
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="common/include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:post:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:post:remove')}]];
    var catalogDatas = [[${@dict.getType('si_service_catalog')}]];
    var packageDatas = [[${@dict.getType('si_car_package')}]];
    var auditDatas = [[${@dict.getType('si_audit_status')}]];
    var saleDatas = [[${@dict.getType('si_sale_status')}]];
    var prefix = ctx + "/business/serviceItem";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            sortName: "seq",
            modalName: "服务单项",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '服务项id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },

                {
                    field: 'originalPrice',
                    title: '服务项原价',
                    sortable: true
                },
                {
                    field: 'discountPrice',
                    title: '服务项折扣价',
                    sortable: true
                },
                {
                    field: 'carPackage',
                    title: '是否套餐',
                    sortable: true,
                    formatter: function (value, row, index) {
                        console.log(value)
                        let val = value ? '是' : '否';
                        return val;
                        //return $.table.selectDictLabel(packageDatas, value);
                    }
                },
                {
                    field: 'info',
                    title: '备注信息',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                /*{
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },*/
                {
                    field: 'serviceCatalog',
                    title: '服务分类',
                    sortable: true,
                    formatter: function (value, row, index) {
                        console.log(catalogDatas)
                        return $.table.selectDictLabel(catalogDatas, value);
                    }
                },
                {
                    field: 'auditStatus',
                    title: '审核状态',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(auditDatas, value);
                    }
                },
                {
                    field: 'saleStatus',
                    title: '上架状态',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(saleDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var disUpdate;
                        var disSaleOn;
                        //已经上架，或者审核中的状态，不允许修改；按钮变灰色
                        if (row.saleStatus == 1 || row.auditStatus == 1) {
                            disUpdate = 'disabled';
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + disUpdate + '"  href="javascript:void(0)" ' +
                            'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');

                        //上架按钮：本身是上架的状态，是否套餐，是套餐，审核通过，
                        if (row.saleStatus == 0) {
                            if (row.carPackage == 1 && row.auditStatus != 2) {
                                disSaleOn = 'disabled';
                            }
                            actions.push('<a  class="btn btn-info btn-xs ' + disSaleOn + '" href="javascript:void(0)" ' +
                                'onclick="saleOn(\'' + row.id + '\')"><i class="fa fa-info"></i>上架</a> ');
                        } else {
                            //下架的条件：上架的状态（显示）；
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" ' +
                                'onclick="saleOff(\'' + row.id + '\')"><i class="fa fa-warning"></i>下架</a> ');
                        }

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

    });

    function saleOn(id) {
        $.modal.confirm("确定要上架吗", function () {
            $.operate.get("/business/serviceItem/saleOn?id=" + id);
        })
    }

    function saleOff(id) {
        $.modal.confirm("确定要下架吗", function () {
            $.operate.get("/business/serviceItem/saleOff?id=" + id);
        })
    }

    $("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
        var rows = $('#bootstrap-table').bootstrapTable('getSelections');
        if (rows.length > 1 || rows.length == 0) {
            $("#examine").addClass("disabled")
            return
        }
        var row = rows[0];
        console.log(row);
        if (row) {
            if ( row.carPackage == 0) {
                $("#examine").addClass("disabled")
                return;
            }

            if (row.auditStatus == 1 || row.auditStatus ==  2) {
                $("#examine").addClass("disabled")
                return;
            }
            $("#examine").removeClass("disabled")
        }
    });

    function startAudit() {
        var rows = $('#bootstrap-table').bootstrapTable('getSelections');
        $.modal.open("修改审核定义","/business/serviceItem/audit?id="+ rows[0].id)
    }
</script>
</body>
</html>